﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Shapes;
using DataForest.DataModel;
using DataForest.Utilities;
using DataForest.ViewModel.Dialogs;

namespace DataForest.ViewModel
{
    public class TableTabModel : TabModel
    {
        public TableTabModel(Workgroup group) : base (group)
        {
        }

        private object selectedCell;
        public object SelectedCell
        {
            get
            {
                return selectedCell;
            }
            set
            {
                if (selectedCell != value)
                {
                    selectedCell = value;
                    OnPropertyChanged("SelectedCell");
                }
            }
        }

        private RelayCommand saveCommand;
        private RelayCommand saveAsCommand;
        private RelayCommand insertRowBeforeCommand;
        private RelayCommand insertRowAfterCommand;
        private RelayCommand insertColBeforeCommand;
        private RelayCommand insertColAfterCommand;
        private RelayCommand setTargetAttributCommand;
        private RelayCommand deleteRowCommand;
        private RelayCommand deleteColCommand;
        private RelayCommand renameColCommand;

        public ICommand SaveCommand
        {
            get
            {
                if (saveCommand == null)
                {
                    saveCommand = new RelayCommand(param => Save());
                }
                return saveCommand;
            }
        }
        public ICommand SaveAsCommand
        {
            get
            {
                if (saveAsCommand == null)
                {
                    saveAsCommand = new RelayCommand(param => SaveAs());
                }
                return saveAsCommand;
            }
        }
        public ICommand InsertRowBeforeCommand
        {
            get
            {
                if (insertRowBeforeCommand == null)
                {
                    insertRowBeforeCommand = new RelayCommand(param => InsertRowBefore(param as DataRowView));
                }
                return insertRowBeforeCommand;
            }
        }
        public ICommand InsertRowAfterCommand
        {
            get
            {
                if (insertRowAfterCommand == null)
                {
                    insertRowAfterCommand = new RelayCommand(param => InsertRowAfter(param as DataRowView));
                }
                return insertRowAfterCommand;
            }
        }
        public ICommand InsertColBeforeCommand
        {
            get
            {
                if (insertColBeforeCommand == null)
                {
                    insertColBeforeCommand = new RelayCommand(param => InsertColumnBefore(param as DataGridColumnHeader));
                }
                return insertColBeforeCommand;
            }
        }
        public ICommand InsertColAfterCommand
        {
            get
            {
                if (insertColAfterCommand == null)
                {
                    insertColAfterCommand = new RelayCommand(param => InsertColumnAfter(param as DataGridColumnHeader));
                }
                return insertColAfterCommand;
            }
        }
        public ICommand SetTargetAttributCommand
        {
            get
            {
                if (setTargetAttributCommand == null)
                {
                    setTargetAttributCommand = new RelayCommand(param => SetTargetAttribut(param as DataGridColumnHeader), param => (param as DataGridColumnHeader != null && (param as DataGridColumnHeader).Column != null) || (SelectedCell != null && SelectedCell is DataGridCellInfo));
                }
                return setTargetAttributCommand;
            }
        }
        public ICommand DeleteRowCommand
        {
            get
            {
                if (deleteRowCommand == null)
                {
                    deleteRowCommand = new RelayCommand(param => DeleteRow(param as DataRowView));
                }
                return deleteRowCommand;
            }
        }
        public ICommand DeleteColCommand
        {
            get
            {
                if (deleteColCommand == null)
                {
                    deleteColCommand = new RelayCommand(param => DeleteCol(param as DataGridColumnHeader), param => (param as DataGridColumnHeader != null && (param as DataGridColumnHeader).Column != null) || (SelectedCell != null && SelectedCell is DataGridCellInfo));
                }
                return deleteColCommand;
            }
        }
        public ICommand RenameColCommand
        {
            get
            {
                if (renameColCommand == null)
                {
                    renameColCommand = new RelayCommand(param => RenameCol(param as DataGridColumnHeader), param => (param as DataGridColumnHeader != null && (param as DataGridColumnHeader).Column != null) || (SelectedCell != null && SelectedCell is DataGridCellInfo));
                }
                return renameColCommand;
            }
        }


        private void DeleteRow(DataRowView rowView)
        {
            if (rowView == null)
            {
                if (SelectedCell is DataGridCellInfo)
                {
                    rowView = ((DataGridCellInfo)SelectedCell).Item as DataRowView;
                }
            }
            if (rowView != null && rowView.Row != null)
            {
                this.Group.Table.Rows.Remove(rowView.Row);
            }
        }

        private void InsertRowBefore(DataRowView rowView)
        {
            if (rowView == null)
            {
                if (SelectedCell != null && SelectedCell is DataGridCellInfo)
                {
                    rowView = ((DataGridCellInfo)SelectedCell).Item as DataRowView;
                }
            }

            if (rowView == null)
            {
                DataRow row = Group.Table.NewRow();
                Group.Table.Rows.InsertAt(row, 0);
            }

            if (rowView != null && rowView.Row != null) {
                
                int pos = Group.Table.Rows.IndexOf(rowView.Row);
                DataRow row = Group.Table.NewRow();
                Group.Table.Rows.InsertAt(row, pos);
            }
        }

        private void InsertRowAfter(DataRowView rowView)
        {
            if (rowView == null)
            {
                if (SelectedCell is DataGridCellInfo)
                {
                    rowView = ((DataGridCellInfo)SelectedCell).Item as DataRowView;
                }
            }


            if (rowView == null)
            {
                DataRow row = Group.Table.NewRow();
                Group.Table.Rows.InsertAt(row, Group.Table.Rows.Count);
            } 
            else if (rowView != null && rowView.Row != null)
            {
                int pos = Group.Table.Rows.IndexOf(rowView.Row);
                DataRow row = Group.Table.NewRow();
                Group.Table.Rows.InsertAt(row, pos+1);
            }
        }

        private void RenameCol(DataGridColumnHeader cell)
        {
            DataGridColumn col = null;
            if (cell == null)
            {
                if (SelectedCell is DataGridCellInfo)
                {
                    col = ((DataGridCellInfo)SelectedCell).Column as DataGridColumn;
                }
            }
            else
            {
                col = cell.Column;
            }

            if (col != null)
            {
                foreach (DataColumn column in Group.Table.Columns)
                {
                    if (column.ColumnName == col.SortMemberPath)
                    {
                        ColumnNameDialogViewModel model = new ColumnNameDialogViewModel();
                        model.ColumnName = column.ColumnName;
                        if (MyDialogService.ShowDialog(model) == true)
                        {
                            if (!String.IsNullOrEmpty(model.ColumnName))
                            {
                                column.ColumnName = model.ColumnName;
                                Group.RefreshTable();
                                return;
                            }
                        }
                    }
                }
            }
        }

        private void InsertColumnAfter(DataGridColumnHeader cell)
        {
            DataGridColumn col = null;
            if (cell == null)
            {
                if (SelectedCell != null && SelectedCell is DataGridCellInfo)
                {
                    col = ((DataGridCellInfo)SelectedCell).Column as DataGridColumn;
                }
            }
            else
            {
                col = cell.Column;
            }

            if (col != null)
            {
                foreach (DataColumn column in Group.Table.Columns)
                {
                    if (column.ColumnName == col.SortMemberPath)
                    {
                        ColumnNameDialogViewModel model = new ColumnNameDialogViewModel();
                        if (MyDialogService.ShowDialog(model) == true)
                        {
                            if (!String.IsNullOrEmpty(model.ColumnName))
                            {
                                int pos = Group.Table.Columns.IndexOf(column);
                                DataColumn newCol = Group.Table.Columns.Add(model.ColumnName, typeof(string));
                                newCol.SetOrdinal(pos + 1);
                                Group.RefreshTable();
                                return;
                            }
                        }
                    }
                }
            }
            else
            {
                ColumnNameDialogViewModel model = new ColumnNameDialogViewModel();
                if (MyDialogService.ShowDialog(model) == true)
                {
                    if (!String.IsNullOrEmpty(model.ColumnName))
                    {
                        int pos = Group.Table.Columns.Count;
                        DataColumn newCol = Group.Table.Columns.Add(model.ColumnName, typeof(string));
                        newCol.SetOrdinal(pos);
                        Group.RefreshTable();
                        return;
                    }
                }
            }
        }

        private void InsertColumnBefore(DataGridColumnHeader cell)
        {
            DataGridColumn col = null;
            if (cell == null)
            {
                if (SelectedCell is DataGridCellInfo)
                {
                    col = ((DataGridCellInfo)SelectedCell).Column as DataGridColumn;
                }
            }
            else
            {
                col = cell.Column;
            }

            if (col != null)
            {
                foreach (DataColumn column in Group.Table.Columns)
                {
                    if (column.ColumnName == col.SortMemberPath)
                    {
                        ColumnNameDialogViewModel model = new ColumnNameDialogViewModel();
                        if (MyDialogService.ShowDialog(model) == true)
                        {
                            if (!String.IsNullOrEmpty(model.ColumnName))
                            {
                                int pos = Group.Table.Columns.IndexOf(column);
                                DataColumn newCol = Group.Table.Columns.Add(model.ColumnName, typeof(string));
                                newCol.SetOrdinal(pos);
                                Group.RefreshTable();
                                return;
                            }
                        }
                    }
                }
            }
            else
            {
                ColumnNameDialogViewModel model = new ColumnNameDialogViewModel();
                if (MyDialogService.ShowDialog(model) == true)
                {
                    if (!String.IsNullOrEmpty(model.ColumnName))
                    {
                        DataColumn newCol = Group.Table.Columns.Add(model.ColumnName, typeof(string));
                        newCol.SetOrdinal(0);
                        Group.RefreshTable();
                        return;
                    }
                }
            }
        }

        private void DeleteCol(DataGridColumnHeader cell)
        {
            DataGridColumn col = null;
            if (cell == null)
            {
                if (SelectedCell is DataGridCellInfo)
                {
                    col = ((DataGridCellInfo)SelectedCell).Column as DataGridColumn;
                }
            }
            else
            {
                col = cell.Column;
            }
            if (col != null)
            {
                foreach (DataColumn column in Group.Table.Columns)
                {
                    if (column.ColumnName == col.SortMemberPath)
                    {
                        Group.Table.Columns.Remove(column);
                        Group.RefreshTable();
                        return;
                    }
                }
            }
        }

        private void SetTargetAttribut(DataGridColumnHeader cell)
        {
            DataGridColumn col = null;
            if (cell == null)
            {
                if (SelectedCell is DataGridCellInfo)
                {
                    col = ((DataGridCellInfo)SelectedCell).Column as DataGridColumn;
                }
            }
            else
            {
                col = cell.Column;
            }
            if (col != null)
            {
                foreach (DataColumn column in Group.Table.Columns) {
                    if (column.ColumnName == col.SortMemberPath)
                    {
                        Group.Table.TargetAttribute = column;
                        return;
                    }
                }
            }
        }

        private void Save()
        {
            SaveFileDialogModel dialog = new SaveFileDialogModel();
            dialog.Title = "Datei speichern";
            dialog.Filter = "Zeichensepariert (*.txt,*.csv)|*.txt; *.csv";

            if (MyDialogService.ShowDialog(dialog) == true)
            {
                this.Group.Table.CreateCSVFile(dialog.FileName, ";");
            }

        }


        private void SaveAs()
        {
            SaveFileDialogModel dialog = new SaveFileDialogModel();
            dialog.Title = "Datei speichern";
            dialog.Filter = "Zeichensepariert (*.txt,*.csv)|*.txt; *.csv";

            if (MyDialogService.ShowDialog(dialog) == true)
            {
                this.Group.Table.CreateCSVFile(dialog.FileName, ";");
            }
        }
    }
}
